package com.anyou.mall.util;

import com.anyou.mall.sho.vo.JuliVo;


import java.math.BigDecimal;

/**
 * 两个经纬度坐标间的距离计算，工具类
 */
public class XYUtil {

    // 椭球的长半轴
    private static double EARTH_RADIUS = 6378.137;

    /**
     * 计算弧度
     */
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 22.534662,114.02964
     * 22.537843,114.023872
     * 通过经纬度计算AB两点间的距离
     * A点经度:longitude1、A点纬度:latitude1、
     * B点经度:longitude2、 B点纬度:latitude2
     */
    public static JuliVo getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
        double radLat1 = rad(latitude1);
        double radLat2 = rad(latitude2);
        double a = radLat1 - radLat2;
        double b = rad(longitude1) - rad(longitude2);
        double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        distance = distance * EARTH_RADIUS;
        BigDecimal decimal = new BigDecimal(distance);
        //结果保留3位小数，此处算出来的单位为km 千米
        distance = decimal.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
        //千米 相乘1000得到米
        BigDecimal aa=new BigDecimal(distance).multiply(new BigDecimal(1000));
        JuliVo juli = new JuliVo("m", aa.intValue());//单位 km为千米，m为米
        return juli;
    }

    public static void main(String[] args) {
        // A点 117.034727 36.665777 B点 117.041006 36.665871
        //* 22.534662,114.02964
        //* 22.537843,114.023872
        JuliVo m = XYUtil.getDistance(22.534662, 114.02964, 22.537843, 114.023872);
        System.out.println(m);//AB直线700米左右
    }


}
